<?php require VIEWPATH . ADMIN_DIR . DIRECTORY_SEPARATOR . 'header.php' ?>
<style type="text/css">

</style>
<div class="page-header">
	<h1 style="font-size: 20px;">	
		成绩项管理
		<a href="#model-result" class="btn btn-info a-custom" data-toggle="modal">
			<i class="ace-icon fa fa-plus"></i> 添加成绩项
		</a>
		</span>
	</h1>
</div>

<div class="col-xs-12">
	<table id="dynamic-table" class="table table-striped table-bordered table-hover" style="margin-top:10px">
		<thead>
			<tr>
				<th>成绩项名称</th>
				<th>排序值</th>
				<th>前台是否显示</th>
				<th>添加时间</th>
				<th>年级项列表</th>
				<th>操作</th>
			</tr>
		</thead>
		<tbody>
		</tbody>
	</table>
</div>
<!-- 成绩项弹窗 -->
<div id="model-result" class="modal fade">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header no-padding">
				<div class="table-header">
					<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
						<span class="white">&times;</span>
					</button> <span id="school-name"></span>
					编辑成绩项
				</div>
			</div>
			<div class="modal-body no-padding">
				<div class="row">
					<div class="col-xs-12">
						<form class="form-horizontal mt10">
							<input type="hidden" name="id" />
							<div class="form-group">
								<label class="col-sm-4 control-label no-padding-right">成绩项名称：</label>
								<div class="col-sm-5">
									<input class="form-control"  type="text" name="title" placeholder="如2019年期末考" />
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-4 control-label no-padding-right">排序（数值越大越靠前）：</label>
								<div class="col-sm-5">
									<input class="form-control"  type="number" name="sort" value="0" />
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-4 control-label no-padding-right">是否显示：</label>
								<div class="col-sm-5">
									<select class="form-control" name="is-show">
										<option value="1">是</option>
										<option value="0">否</option>
									</select>
								</div>
							</div>
						</form>
					</div>
				</div>
			</div>
			<div class="modal-footer no-margin-top" >
				<a href="javascript:;" class="btn btn-sm btn-primary" id="save-result-major">
					<i class="ace-icon fa fa-check"></i> 保存
				</a>
			</div>
		</div>
	</div>
</div>

<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'footer.php'?>
<script type="text/javascript">
	var table = "";
	$(function(){
		// 成绩成绩项列表
		table = $('#dynamic-table').dt({
			"ajaxUrl": '<?= admin_url('contents/get_result_major_list')?>',
			"columns": [	
				{"data": function(ret){
	            	return ret.title;
				}},
				{"data": function(ret){
	            	return ret.sort;
				}},
				{"data": function(ret){
	            	return ret.is_show_chinese;
				}},
				{"data": function(ret){
	            	return ret.createtime;
				}},
				{"data": function(ret){
	            	return '<a href="<?= admin_url('contents/result_grade_data/')?>' + ret.id + '" class="mr10"><i class="fa fa-gears"></i> 查看</a>';
				}},
				{"data": function(ret){
					var action = '';
					action += '<a data-id="'+ ret.id + '" data-title="'+ ret.title + '" data-sort="'+ ret.sort + '" data-isshow="'+ ret.is_show + '" href="#model-result"" data-toggle="modal" class="mr10 text-success edit-result-major"><i class="fa fa-edit"></i> 编辑</a>&nbsp;&nbsp;';
					action += '<a data-id="'+ ret.id + '" data-title="'+ ret.title + '" class="delete-result-major text-danger" href="javascript:;"><i class="fa fa-times"></i> 删除</a>';
					return action;
				}}
			]
		});
		// 修改成绩项标题时,传递旧值
		$("body").on("click", ".edit-result-major", function(){
			var id = $(this).data("id");
			var title = $(this).data("title");
			var sort = $(this).data("sort");
			var isShow = $(this).data("isshow");
			$("[name=id]").val(id);
			$("[name=title]").val(title);
			$("[name=sort]").val(sort);
			$("[name=is-show]").val(isShow);
		});
		// 保存成绩项
		$("#save-result-major").click(function(){
			var submitUrl = "<?=admin_url('contents/save_result_major_action')?>";
			// 成绩项id
			var id = $('[name=id]').val();
			// 成绩项标题
			var title = $('[name=title]').val();
			var sort = $('[name=sort]').val();
			var isShow = $('[name=is-show]').val();
			var data = {
				id: id,
				title: title,
				sort: sort,
				is_show: isShow,
			};
			ajax_post(submitUrl, data, function(){
				// 刷新页面
				location.reload();
			});
		});
		// 删除某成绩项
		$("body").on("click", ".delete-result-major", function(){
			var id = $(this).data("id");
			var title = $(this).data("title");
			layer.confirm('确定删除:' + title + '？', function(){
				// 提交数据
				var postUrl = "<?=admin_url('contents/delete_result_major_action')?>";
				var postData = { id: id };
				ajax_post(postUrl, postData, function(res){
					table.draw(false);
				})
			});
		})
	})
</script>